<?php

namespace Xrfg\Api\Consume\Console;

use Exception;
use Throwable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Xrfg\Api\Consume\Models\ConsumeAccounts;
use Xrfg\Core\Trading\Account\Models\Account as TradingAccount;
use Xrfg\Api\Consume\Models\ConsumeOrders;

class ConsumeConsole extends Command
{
  
    public $signature = 'daily_revenue';

    public $description = '每日收益结算';

    public function handle()
    {
        // 查询冻结XG收益账户
        $consumeAccounts = ConsumeAccounts::where("disabled",">",0)->get();
        foreach ($consumeAccounts as $v) {
            try {
                logger($v->user_id.'结算XG收益');
                // 查询释放比例
                $rate = 0.005;
                $number = $v->disabled*$rate;
                if($v->coin_id == 1){
                    $rate = $this->releaseRate($v->user_id);
                    // 查询用户优惠券金额
                    $order_sum = ConsumeOrders::where('user_id',$v->user_id)->sum('number');
                    $number = $order_sum * 100 * $rate;
                }
                // 释放冻结收益
                $consumeAccount = ConsumeAccounts::firstOrCreate([
                    'user_id' => $v->user_id,
                    'coin_id' => $v->coin_id,
                ])->refresh();
                $consumeAccount = ConsumeAccounts::lockForUpdate()->find($consumeAccount->id);
                $consumeAccount->disabledDecrement($number);
                $consumeAccount->type = '释放冻结收益';
                $consumeAccount->action_id = $v->user_id;
                $consumeAccount->save();
                 // 生成收益流水记录
                $account = TradingAccount::firstOrCreate([
                    'user_id' => $v->user_id,
                    'coin_id' => $v->coin_id,
                ])->refresh();
                $account = TradingAccount::lockForUpdate()->find($account->id);
                $account->availableIncrement($number);
                $account->proofId = [$v->user_id, 3,9];
                $account->save();
                if($v->coin_id == 1){
                    // 是否退还完U
                    if($consumeAccount->disabled == 0){
                        // 退还质押xg
                        $xgConsumeAccount = ConsumeAccounts::firstOrCreate([
                            'user_id' => $v->user_id,
                            'coin_id' => 2,
                        ])->refresh();
                        $xgConsumeAccount->freezeDecrement($xgConsumeAccount->freeze);
                        $xgConsumeAccount->type = '退还质押';
                        $xgConsumeAccount->action_id = $v->user_id;
                        $xgConsumeAccount->save();
                         // 生成收益流水记录
                        $xgAccount = TradingAccount::firstOrCreate([
                            'user_id' => $v->user_id,
                            'coin_id' => 2,
                        ])->refresh();
                        $xgAccount = TradingAccount::lockForUpdate()->find($xgAccount->id);
                        $xgAccount->availableIncrement($number);
                        $xgAccount->proofId = [$v->user_id, 3,10];
                        $xgAccount->save();       
                    }
                }
            } catch (Throwable $exception) {
                Log::debug($exception);
                return;
            }
        }
    }

    // 查询释放比例
    protected function releaseRate($user_id)
    {
        logger('计算'.$user_id.'直推人数');
        // 购买订单
        $u = ConsumeOrders::where('parent_id', $user_id)->select('user_id')->distinct()->pluck('user_id')->toArray();
        logger('直推人数：'.count($u));
        if(count($u) == 0){
            $rate = 0.005;
        }elseif(count($u) == 1){
            $rate = 0.006;
        }elseif(count($u) == 2){
            $rate = 0.007;
        }elseif(count($u) == 3){
            $rate = 0.008;
        }elseif(count($u) == 4){
            $rate = 0.009;
        }elseif(count($u) == 5){
            $rate = 0.010;
        }else{
            $rate = 0.011;
        }
        return $rate;
    }
}
